package com.dream.dao.master;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import org.springframework.stereotype.Repository;

import com.dream.model.master.Product;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.master.ProductVO;

@Repository
public class ProductDAO extends DreamBaseDAO<Product> {
	
	public PagingVO query(final ProductVO vo, final PagingVO page){
		
		DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
		
	}
	
	private DetachedCriteriaBuilder initQueryBuilder(final ProductVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(Product.class);
		builder.addEq("deleted", false).addLikeAny("name", vo.getName());
		builder.addIn("categoryId", vo.getCategories());
		
		return builder;
		
	}
	
	public Product search(String name,Long cat){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(Product.class);
		builder.addEq("deleted", false).addEq("name", name);
		builder.addEq("categoryId", cat);
		return this.selectTopOne(builder);
		
	}
	
}
